<?php
	class LoaiGiuong extends CI_Controller
	{
		function admin_index()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$this->load->model('loaigiuong_model');
				$data['loaigiuong'] = $this->loaigiuong_model->LayTatCaLoaiGiuong();
				
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');
			
				$this->load->view('admin/loaigiuong_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		function Tim()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = urldecode($this->uri->segment(3));
					
				$this->load->model('loaigiuong_model');
				$data['loaigiuong'] = $this->loaigiuong_model->Tim($key);
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/loaigiuong_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function LayLoaiGiuong_ajax()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$key = $this->input->post('name_startsWith');
				
				$this->load->model('loaigiuong_model');
				$data['malg'] = $this->loaigiuong_model->LayMaLG_ajax($key);
				$data['tenlg'] = $this->loaigiuong_model->LayTenLG_ajax($key);
				
				$this->load->view('admin/ajax_danhsachloaigiuong', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function Xoa()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$malg = $this->uri->segment(3);
				if($malg)
				{
					$this->load->model('loaigiuong_model');
					$result = $this->loaigiuong_model->Xoa($malg);
					if($result)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	            	    <script>
							alert("Loại giường đã được xóa!");
							window.location = "<?php echo base_url(); ?>index.php/loaigiuong/admin_index";
						</script>
	                	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	    	            <script>
							alert("Xóa loại giường thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/loaigiuong/admin_index";
						</script>
	    	            <?php
					}			
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function Them()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$tenlg = $this->input->post('tenlg');
				$loaigiuong = array('Ten' => $tenlg);
								
				$this->load->model('loaigiuong_model');
				$trungten = $this->loaigiuong_model->KiemTraTrungTen($tenlg);
				
				if($trungten)
				{
					echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
					?>
           		    <script>
						alert("Thêm loại giường thất bại. Loại giường \"<?php echo $tenlg; ?>\" đã có!");
						window.location = "<?php echo base_url(); ?>index.php/loaigiuong/admin_index";
					</script>
    	           	<?php
				}
				else
				{				
					$result = $this->loaigiuong_model->Them($loaigiuong);
				
					if($result)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
            		    <script>
							alert("Loại giường đã được thêm!");
							window.location = "<?php echo base_url(); ?>index.php/loaigiuong/admin_index";
						</script>
    	            	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
    		            <script>
							alert("Thêm loại giường thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/loaigiuong/admin_index";
						</script>
    	            	<?php
					}
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function MoTrangCapNhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$malg = $this->uri->segment(3);
				if($malg)
				{
					$this->load->model('loaigiuong_model');
					$data['loaigiuong'] = $this->loaigiuong_model->LayLoaiGiuong($malg);
															
					$data['key'] = NULL;
				
					$data['chucvu'] = $this->session->userdata('chucvu');
				
					$data['current_username'] = $this->session->userdata('username');		
					
					$this->load->view('admin/capnhat_loaigiuong_view', $data);
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function CapNhat()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				
				$malg = $this->input->post('malg');
				$data = array('Ten' => $this->input->post('tenlg'));
				$this->load->model('loaigiuong_model');
				$result = $this->loaigiuong_model->CapNhat($data, $malg);
								
				if($result < 0)
				{
					?>
                    <script>
						alert('Thay đổi thông tin thất bại!');
						window.location = "<?php echo base_url(); ?>index.php/loaigiuong/MoTrangCapNhat/<?php echo $malg; ?>";						
					</script>
                    <?php
					return;
				}
				
				?>
                <script>
					alert('Thông tin đã được thay đổi!');
					window.location = "<?php echo base_url(); ?>index.php/loaigiuong/MoTrangCapNhat/<?php echo $malg; ?>";						
				</script>
                <?php				
			}
			else
				redirect('login/admin_index');			
		}
	}
?>